import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:old_shop/login&regist/view/button_view.dart';
import 'package:old_shop/login&regist/view/normal_text.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../common/const/cosnt.dart';

class PwdSettingPage extends StatefulWidget {
  final Map params;

  const PwdSettingPage({Key? key, required this.params}) : super(key: key);
  @override
  _PwdSettingPageState createState() => _PwdSettingPageState();
}

class _PwdSettingPageState extends State<PwdSettingPage> {
  final _pwdController = TextEditingController();
  final _inviteCodeController = TextEditingController();
  final _pwdFocus = FocusNode();
  final _inviteCodeFocus = FocusNode();

  @override
  void dispose() {
    _pwdController.dispose();
    _inviteCodeController.dispose();
    super.dispose();
  }

  void _jumpToRoot() {
    SharedPreferences.getInstance().then((value) => value.setBool(AppConst.APP_IS_LOGIN, true));
    Navigator.of(context).pushNamedAndRemoveUntil('/root', (Route<dynamic> route) => false);
  }

  void _unfocus() {
    _pwdFocus.unfocus();
    _inviteCodeFocus.unfocus();
  }

  void _login() async {
    EasyLoading.show(status: "Loading...");
    // 注册或者登录
    await Future.delayed(const Duration(seconds: 1));
    EasyLoading.dismiss();
    _jumpToRoot();
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final marginH = size.height * 0.06;

    return Scaffold(
      backgroundColor: Colors.white,
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: _unfocus,
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.arrow_back_ios),
                onPressed: () => Navigator.pop(context),
              ),
              SizedBox(
                height: marginH,
              ),
              Padding(
                padding: const EdgeInsets.all(20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      "请设置密码",
                      style: AppFont.textStyle(25, color: AppColor.font333, fontWeight: FontWeight.bold),
                    ),
                    SizedBox(height: 8),
                    Text(
                      "8-20个字符，不可以是纯数字",
                      style: AppFont.textStyle(13, color: Colors.grey),
                    ),
                    SizedBox(
                      height: marginH,
                    ),
                    NormalTextfield(
                      hintText: "请输入密码",
                      obscureText: true,
                      controller: _pwdController,
                      focusNode: _pwdFocus,
                      maxLength: 20,
                    ),
                    InkWell(
                      onTap: _login,
                      child: BottomButton(
                        margin: const EdgeInsets.only(top: 35),
                        title: "完成",
                      ),
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
